''' <summary>
''' This is the main type for your game
''' </summary>
Public Class Game1
    Inherits Microsoft.Xna.Framework.Game

    Private WithEvents graphics As GraphicsDeviceManager
    Private WithEvents spriteBatch As SpriteBatch

    Private ContentDir As String = System.AppDomain.CurrentDomain.BaseDirectory & "Content\"
    Private BaseEffect As Effect

    Private MapObject(0) As MapObjects.Models
    Private Camera As Cameras.FirstPerson

    Public Sub New()
        graphics = New GraphicsDeviceManager(Me)
        Content.RootDirectory = "Content"
    End Sub

    ''' <summary>
    ''' Allows the game to perform any initialization it needs to before starting to run.
    ''' This is where it can query for any required services and load any non-graphic
    ''' related content.  Calling MyBase.Initialize will enumerate through any components
    ''' and initialize them as well.
    ''' </summary>
    Protected Overrides Sub Initialize()
        ' TODO: Add your initialization logic here
        MyBase.Initialize()
    End Sub

    ''' <summary>
    ''' LoadContent will be called once per game and is the place to load
    ''' all of your content.
    ''' </summary>
    Protected Overrides Sub LoadContent()
        ' Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = New SpriteBatch(GraphicsDevice)

        Dim FS As New System.IO.FileStream(ContentDir & "MainFX.bin", IO.FileMode.Open)
        Dim bLen As Integer
        bLen = CInt(FS.Length - 1)
        Dim Bytes(bLen) As Byte
        FS.Read(Bytes, 0, bLen)
        FS.Close()
        FS = Nothing
        BaseEffect = New Effect(graphics.GraphicsDevice, Bytes)

        Camera = New Cameras.FirstPerson(graphics, New Vector3(0, 0, -5.5))
        MapObject(0) = New MapObjects.Models(graphics, ContentDir)
        MapObject(0).ImportOBJFile("HumanFemale_2b", 10)
        MapObject(0).ModelLocation = New Vector3(0, 0, 0)

        'MapObject(1) = New Processors.OBJMesh(graphics, ContentDir)
        'MapObject(1).ImportOBJFile(ContentDir & "HumanFemale_Torso_1.obj")
        'FS = New System.IO.FileStream(ContentDir & "HumanFemale_Torso_1.jpg", IO.FileMode.Open)
        'MapObject(1).ModelTexture = Texture2D.FromStream(GraphicsDevice, FS)
        'FS.Close()
        'MapObject(1).ModelLocation = New Vector3(0, 0, -49)


        ' TODO: use Me.Content to load your game content here
    End Sub

    ''' <summary>
    ''' UnloadContent will be called once per game and is the place to unload
    ''' all content.
    ''' </summary>
    Protected Overrides Sub UnloadContent()
        ' TODO: Unload any non ContentManager content here
    End Sub

    ''' <summary>
    ''' Allows the game to run logic such as updating the world,
    ''' checking for collisions, gathering input, and playing audio.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Protected Overrides Sub Update(ByVal gameTime As GameTime)
        ' Allows the game to exit
        If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
            Me.Exit()
        ElseIf Keyboard.GetState.IsKeyDown(Keys.Escape) = True Then
            Me.Exit()
        End If

        Dim timeDiff As Single
        timeDiff = gameTime.ElapsedGameTime.TotalMilliseconds / 1000
        Camera.Update(timeDiff, Keyboard.GetState, Mouse.GetState)
        MapObject(0).Update(timeDiff, Keyboard.GetState, Mouse.GetState)
        ' TODO: Add your update logic here
        MyBase.Update(gameTime)
    End Sub

    ''' <summary>
    ''' This is called when the game should draw itself.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Protected Overrides Sub Draw(ByVal gameTime As GameTime)
        GraphicsDevice.Clear(Color.CornflowerBlue)
        Dim i As Integer
        Dim RS As New RasterizerState
        RS.CullMode = CullMode.None
        RS.FillMode = FillMode.Solid
        GraphicsDevice.RasterizerState = RS
        ' TODO: Add your drawing code here
        For i = 0 To MapObject.Count - 1
            MapObject(i).Draw(GraphicsDevice, BaseEffect, Camera, New Vector3(-0.5, -1, -0.5), 0.4)

        Next i

        MyBase.Draw(gameTime)
    End Sub

End Class
